package com.qlylkj.lbk.app

import android.app.Application
import android.content.Context
import com.jeremyliao.liveeventbus.LiveEventBus
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.forest.core.util.DeviceUtil
import com.forest.core.util.Preference
import com.qlylkj.lbk.network.NetworkHelper
import com.qlylkj.lbk.wxapi.WeChatConstant
import com.scwang.smartrefresh.header.MaterialHeader
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.tencent.bugly.Bugly
import me.jessyan.autosize.AutoSizeConfig
import kotlin.properties.Delegates

class BaseApplication: Application() {

    companion object{
        var context: Context by Delegates.notNull()
        private var mIWeChatApi: IWXAPI? = null
        fun getIWeChatApi(success: (IWXAPI) -> Unit = {}, failed: () -> Unit = {}) {
            mIWeChatApi?.let {
                success(it)
            } ?: failed()
        }
    }

    init {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColorsId(android.R.color.white)//全局设置主题颜色
            return@setDefaultRefreshHeaderCreator MaterialHeader(context)
        }
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ ->
            //指定为经典Footer，默认是 BallPulseFooter
            return@setDefaultRefreshFooterCreator ClassicsFooter(context).apply {
                setDrawableSize(20f)
            }
        }
    }

    override fun onCreate() {
        super.onCreate()
        context = applicationContext
        Preference.setContext(applicationContext)
        LiveEventBus
            .config()
            .lifecycleObserverAlwaysActive(false)
        DeviceUtil.setContext(applicationContext)
        AutoSizeConfig.getInstance().apply {
            isCustomFragment = true
            isExcludeFontScale = true
        }
        NetworkHelper.initNetWorkCallback(applicationContext)
        initBugly()
//        registerToWeChat()
    }

    private fun registerToWeChat(){
        mIWeChatApi = WXAPIFactory.createWXAPI(this, WeChatConstant.APP_ID, true)
        mIWeChatApi?.registerApp(WeChatConstant.APP_ID)
    }

    private fun initBugly(){
        Bugly.init(applicationContext, Constant.BUGLY_APP_ID, false)
    }

}